package model

import "gorm.io/gorm"

type Picture struct {
	Model
	Title      string `json:"title,omitempty"`
	ImageUrl   string `json:"image_url,omitempty"`
	Desc       string `json:"desc,omitempty"`
	Public     bool   `json:"public"`
	UserID     uint   `json:"user_id,omitempty"`
	CategoryID uint   `json:"category_id,omitempty"`
}

func (p Picture) TableName() string {
	return "img_picture"
}

func (p Picture) Create(db *gorm.DB) error {
	return db.Create(&p).Error
}

func (p Picture) Update(db *gorm.DB) error {
	return db.Model(&p).Updates(&p).Error
}

func (p Picture) UpdatePublic(db *gorm.DB) error {
	return db.Model(&p).Select("public").Updates(&p).Error
}

func (p Picture) Delete(db *gorm.DB) error {
	return db.Delete(&p).Error
}

func (p Picture) Get(db *gorm.DB) (Picture, error) {
	var picture Picture
	err := db.Where(&p).First(&picture).Error
	return picture, err
}

func (p Picture) List(db *gorm.DB, pageOffset int, pageSize int) ([]*Picture, error) {
	var pictures []*Picture
	err := db.Where(&p).Offset(pageOffset).Limit(pageSize).Find(&pictures).Error
	return pictures, err
}

func (p Picture) ListInID(db *gorm.DB, ids [][]interface{}, pageOffset int, pageSize int) ([]*Picture, error) {
	var pictures []*Picture
	err := db.Where("(id) IN ?", ids).Find(&pictures).Error
	return pictures, err
}

func (p Picture) Count(db *gorm.DB) (int, error) {
	var count int64
	err := db.Model(&p).Where(&p).Count(&count).Error
	return int(count), err
}
